#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#pragma warning(disable : 6031)
#include "seqlist.h"

void SeqListInit(SeqList* ps)
{
	ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 10);
	if (ps->a == NULL)
	{
		perror("malloc");
		return;
	}
	ps->size = 0;
	ps->capacity = 10;
}


void SeqListDestroy(SeqList* ps)
{
	free(ps->a);
	ps->a = NULL;
}

int CheckSeqList(SeqList* ps)
{
	if (ps->capacity == ps->size)
	{
		SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * (ps->capacity * 2));
		if (tmp == NULL)
		{
			perror("realloc");
			return 0;
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
	return 1;
}


void SeqListPushBack(SeqList* ps, SLDateType x)
{
	if (CheckSeqList(ps) == 0)
	{
		return;
	}

	ps->a[ps->size] = x;
	ps->size++;
}

void SeqListPrint(SeqList* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
}

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	if (CheckSeqList(ps) == 0)
	{
		return;
	}
	/*int i = 0;
	for (i = ps->size - 1; i >= 0; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[0] = x;
	ps->size++;*/

	/*memmove(ps->a + 1, ps->a, sizeof(SLDateType) * ps->size);
	ps->a[0] = x;
	ps->size++;*/

	SeqListInsert(ps, 0, x);
	
}


void SeqListPopFront(SeqList* ps)
{
	//assert(ps->size != 0);
	/*int i = 0;
	for (i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;*/

	/*memmove(ps->a, ps->a + 1, sizeof(SLDateType) * (ps->size - 1));
	ps->size--;*/
	SeqListErase(ps, 0);
}

void SeqListPopBack(SeqList* ps)
{
	/*assert(ps->size != 0);
	ps->size--;*/

	SeqListErase(ps , ps ->size - 1);
}


int SeqListFind(SeqList* ps, SLDateType x)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}


void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	int i = 0;
	assert(ps->size >= pos);
	/*for (i = ps->size - 1; i >= pos; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[pos] = x;
	ps->size++;*/

	memmove(ps->a + pos + 1, ps->a + pos, sizeof(SLDateType)*(ps->size - pos));
	ps->a[pos] = x;
	ps->size++;
}

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps->size != 0);
	int i = 0;
	for (i = pos; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}